package pl.edu.agh.uddiProxy;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

import pl.edu.agh.uddiProxy.dao.DAOHelper;
import dk.itst.uddi.client.query.FindBindingResult;
import dk.itst.uddi.client.types.core.BindingTemplate;
import dk.itst.uddi.client.types.core.TModelInstanceInfo;

public class ParametersManager implements Job {

	public void execute(JobExecutionContext context) throws JobExecutionException {
		UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("admin", "admin");
		AuthenticationHelper.login(authentication);
		
		FindBindingResult bindingResult = DAOHelper.getUDDIClient().getInquiryClient().findBinding().execute();
		for (BindingTemplate bindingTemplate : bindingResult.getTemplates()) {
			if (bindingTemplate.getTModelInstanceDetails() != null) {
				for (TModelInstanceInfo instanceInfo : bindingTemplate.getTModelInstanceDetails().getTModelInstanceInfoArray()) {
					
					DAOHelper.getParametersMap().update(instanceInfo.getTModelKey());
				}
			}
		}
		
		AuthenticationHelper.logout(authentication);
	}
}
